import java.util.ArrayList;
import java.util.Collections;
import java.util.Scanner;

class GameRound {
    int stone_count;
    int move_limit;
    NimPlayer p1;
    NimPlayer p2;
}

class NimPlayer implements Comparable<NimPlayer> {
    //昵称
    String username;
    //姓
    String familyname;
    //名
    String givenname;
    //移动石子数量
    int move_count;
    //参与游戏对局数
    int game_count;
    //获胜对局数
    int win_count;
    //胜率
    double win_pro;

    //计算胜率
    public double getWin_pro() {
        return game_count == 0 ? 0 : (win_count * 1.0 / game_count) * 100;
    }

    public NimPlayer(String username, String familyname, String givenname) {
        this.username = username;
        this.familyname = familyname;
        this.givenname = givenname;
    }

    //设置移动石子数量
    public void setMove_count(int move_count) {
        this.move_count = move_count;
    }

    //获取移动石子数量
    public int removeStone() {
        return move_count;
    }

    //重写equals方法，判断两个玩家对象是否相同
    public boolean equals(Object obj) {
        if (obj == null) {
            return false;
        }
        if (this == obj) {
            return true;
        }
        if (obj instanceof NimPlayer) {
            NimPlayer nimPlayer = (NimPlayer) obj;
            return nimPlayer.username.equals(this.username);
        }
        return false;
    }

    //重写compareTo方法，排序
    public int compareTo(NimPlayer obj) {
        if(this.win_count>obj.win_count)
            return -1;
        else if(this.win_count==obj.win_count){
            if(this.game_count>obj.game_count)
                return -1;
            else
                return 1;
        }
        else
            return 1;
    }
}

public class Main {

    //打印石头个数
    static void print_stone(int remain) {
        if (remain > 1)
            System.out.print(remain + " stones left:");
        else
            System.out.print(remain + " stone left:");
        for (int i = 0; i < remain; i++) {
            System.out.print(" *");
        }
        System.out.println();
    }

    //判断胜负
    static void judge(int remain, NimPlayer obj) {
        if (remain == 0) {
            System.out.println("Game Over");
            System.out.println(obj.givenname + " " + obj.familyname + " wins!\n");
            obj.win_count++;
        }
    }

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        ArrayList<NimPlayer> list = new ArrayList<>();
        String select;
        System.out.println("Welcome to Nim\n");
        String statue = "hehe";
        do {
            select = sc.next();
            switch (select) {
                case "addplayer": {
                    String[] arr = sc.next().split(",");
                    NimPlayer p = new NimPlayer(arr[0], arr[1], arr[2]);
                    if (list.contains(p)) {
                        System.out.println("The player already exists.\n");
                    } else {
                        list.add(p);
                        p.game_count = 0;
                        p.win_count = 0;
                    }
                    break;
                }
                case "removeplayer": {
                    String username = sc.next();
                    //指定输入字符串“null”代表输入的username为空
                    if (username.equals("null")) {
                        System.out.println("Are you sure you want to remove all players? (Y/N)\n");
                        String elect = sc.next();
                        if (elect.equals("Y"))
                            list.clear();
                    } else {
                        int flag = 1;
                        for (int i = 0; i < list.size(); i++) {
                            if (username.equals(list.get(i).username)) {
                                list.remove(list.get(i));
                                flag = 0;
                                break;
                            }
                        }
                        if (flag == 1)
                            System.out.println("The player does not exist.\n");
                    }
                    break;
                }
                case "editplayer": {
                    String[] arr = sc.next().split(",");
                    NimPlayer p = new NimPlayer(arr[0], arr[1], arr[2]);
                    int flag = 1;
                    for (NimPlayer nimPlayer : list) {
                        if (arr[0].equals(nimPlayer.username)) {
                            nimPlayer.familyname = arr[1];
                            nimPlayer.givenname = arr[2];
                            flag = 0;
                            break;
                        }
                    }
                    if (flag == 1)
                        System.out.println("The player does not exist.\n");
                    break;
                }
                case "resetstats": {
                    String username = sc.next();
                    //指定输入字符串“null”代表输入的username为空
                    if (username.equals("null")) {
                        System.out.println("Are you sure you want to reset all player statistics? (Y/N)\n");
                        String elect = sc.next();
                        if (elect.equals("Y")) {
                            for (NimPlayer nimPlayer : list) {
                                nimPlayer.game_count = 0;
                                nimPlayer.win_count = 0;
                            }
                        }
                    } else {
                        int flag = 1;
                        for (NimPlayer nimPlayer : list) {
                            if (username.equals(nimPlayer.username)) {
                                nimPlayer.game_count = 0;
                                nimPlayer.win_count = 0;
                                flag = 0;
                                break;
                            }
                        }
                        if (flag == 1)
                            System.out.println("The player does not exist.\n");
                    }
                    break;
                }
                case "displayplayer": {
                    String username = sc.next();
                    //指定输入字符串“null”代表输入的username为空
                    if (username.equals("null")) {
                        for (NimPlayer nimPlayer : list) {
                            System.out.println(nimPlayer.username + "," +
                                    nimPlayer.givenname + "," + nimPlayer.familyname
                                    + "," + nimPlayer.game_count + " games," +
                                    nimPlayer.win_count + " wins");
                        }
                    } else {
                        int flag = 1;
                        for (NimPlayer nimPlayer : list) {
                            if (username.equals(nimPlayer.username)) {
                                System.out.println(nimPlayer.username + "," +
                                        nimPlayer.givenname + "," + nimPlayer.familyname
                                        + "," + nimPlayer.game_count + " games," +
                                        nimPlayer.win_count + " wins");
                                flag = 0;
                                break;
                            }
                        }
                        if (flag == 1)
                            System.out.println("The player does not exist.\n");
                    }
                    break;
                }
                case "rankings": {
                    String elect = sc.next();
                    //指定输入字符串“null”代表默认排序
                    if (elect.equals("desc")||elect.equals("null")) {
                        Collections.sort(list);
                        for (int i = 0; i <list.size(); i++) {
                            if (list.get(i).getWin_pro() == 0.0)
                                System.out.printf("0%%   | 0%d games | %s %s\n",
                                        list.get(i).game_count,
                                        list.get(i).givenname, list.get(i).familyname);
                            else
                                System.out.printf("%3.0f%% | 0%d games | %s %s\n",
                                        list.get(i).getWin_pro(), list.get(i).game_count,
                                        list.get(i).givenname, list.get(i).familyname);
                        }
                        System.out.println();
                    }
                    if (elect.equals("asc")) {
                        for (int i = list.size() - 1; i >= 0; i--) {
                            if (list.get(i).getWin_pro() == 0.0)
                                System.out.printf("0%%   | 0%d games | %s %s\n",
                                        list.get(i).game_count,
                                        list.get(i).givenname, list.get(i).familyname);
                            else
                                System.out.printf("%3.0f%% | 0%d games | %s %s\n",
                                        list.get(i).getWin_pro(), list.get(i).game_count,
                                        list.get(i).givenname, list.get(i).familyname);
                        }
                        System.out.println();
                    }
                    break;
                }
                case "startgame": {
                    NimPlayer p1 = null;
                    NimPlayer p2 = null;
                    String[] arr = sc.next().split(",");
                    int initial = Integer.parseInt(arr[0]);
                    int upper = Integer.parseInt(arr[1]);
                    String name1 = arr[2];
                    String name2 = arr[3];
                    int flag = 1;
                    for (NimPlayer nimPlayer : list) {
                        if (name1.equals(nimPlayer.username)) {
                            p1 = nimPlayer;
                            p1.game_count++;
                            flag = 0;
                            break;
                        }
                    }
                    if (flag == 1) {
                        System.out.println("One of the players does not exist.\n");
                        break;
                    }
                    flag = 1;
                    for (NimPlayer nimPlayer : list) {
                        if (name2.equals(nimPlayer.username)) {
                            p2 = nimPlayer;
                            p2.game_count++;
                            flag = 0;
                            break;
                        }
                    }
                    if (flag == 1) {
                        System.out.println("One of the players does not exist.\n");
                        break;
                    }
                    System.out.println("Initial stone count: " + initial);
                    System.out.println("Maximum stone removal: " + upper);
                    System.out.println("Player 1: " + p1.givenname + " " + p1.familyname);
                    System.out.println("Player 2: " + p2.givenname + " " + p2.familyname + "\n");
                    print_stone(initial);
                    while (initial > 0) {
                        System.out.println(p1.givenname + "'s turn - remove how many?");
                        int move_count = sc.nextInt();
                        System.out.println(move_count + "\n");
                        if (move_count > upper || move_count < 1) {
                            if (initial == 1)
                                System.out.println("Invalid move. You must remove between 1 and 1 stones.\n");
                            else
                                System.out.println("Invalid move. You must remove between 1 and 3 stones.\n");
                            print_stone(initial);
                            System.out.println(name1 + "'s turn - remove how many?");
                            move_count = sc.nextInt();
                            System.out.println(move_count + "\n");
                            p1.setMove_count(move_count);
                        } else
                            p1.setMove_count(move_count);
                        initial -= p1.removeStone();
                        judge(initial, p2);
                        if (initial > 0)
                            print_stone(initial);
                        else
                            break;
                        System.out.println(p2.givenname + "'s turn - remove how many?");
                        move_count = sc.nextInt();
                        System.out.println(move_count + "\n");
                        if (move_count > upper || move_count < 1) {
                            if (initial == 1)
                                System.out.println("Invalid move. You must remove between 1 and 1 stones.\n");
                            else
                                System.out.println("Invalid move. You must remove between 1 and 3 stones.\n");
                            print_stone(initial);
                            System.out.println(p2.givenname + "'s turn - remove how many?");
                            move_count = sc.nextInt();
                            System.out.println(move_count + "\n");
                            p2.setMove_count(move_count);
                        } else
                            p2.setMove_count(move_count);
                        initial -= p2.removeStone();
                        judge(initial, p1);
                        if (initial > 0)
                            print_stone(initial);
                        else
                            break;
                    }
                    break;
                }
                case "exit": {
                    statue = "exit";
                    break;
                }
            }
        } while (!statue.equals("exit"));
    }
}


